package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.ContainerLock;
import com.biomatch.domain.Fingerprint;
import com.biomatch.domain.RoleUser;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.mapper.FingerprintMapper;
import com.biomatch.mapper.RoleUserMapper;
import com.biomatch.mapper.UserMapper;
import com.biomatch.service.UserService;
import com.biomatch.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FingerprintMapper fingerprintMapper;

    @Override
    public UserInfo loginByPassword(UserInfo userInfo) {
        return userMapper.loginByPassword(userInfo);
    }

    @Override
    public List<UserInfo> getUserList() {
        List<UserInfo> userInfoList = userMapper.selectList(new QueryWrapper<>());
        for (UserInfo userInfo : userInfoList) {
            userInfo.setLgnpwd(null);
        }
        return userInfoList;
    }

    @Override
    public UserInfo saveOrUpdateUser(UserInfo userInfo) {
        if (StringUtils.isEmpty(userInfo.getUsrcod())) {
            if (userMapper.selectOneByName(userInfo.getLgnnam()) != null) {
                return null;
            }
        } else {
            if (userMapper.selectOneNotCodeIsName(userInfo.getUsrcod(), userInfo.getLgnnam()) != null) {
                return null;
            }
        }
        if (StringUtils.isEmpty(userInfo.getUsrcod())) {
            userInfo.setUsrcod("U" + CommonUtils.getRamdonStr());
            userMapper.insert(userInfo);
            RoleUser roleUser = new RoleUser(userInfo.getRole().getRoleId(), userInfo.getUsrcod());
            roleUserMapper.insert(roleUser);

            List<Fingerprint> fingerprints = userInfo.getFingerprintList();
            for (Fingerprint fingerprint : fingerprints) {
                //如果id<=0 判断增加
                if(fingerprint.getId()<=0)
                {
                    fingerprint.setCreateTime(new Date());
                    fingerprint.setUserId(userInfo.getUsrcod());
                    fingerprintMapper.insert(fingerprint);
                }
            }
            
        } else {
            userInfo.setUpdtim(new Date());
            UpdateWrapper<UserInfo> userInfoUpdateWrapper = new UpdateWrapper<>();
            userInfoUpdateWrapper.eq("usrcod", userInfo.getUsrcod());
            userMapper.update(userInfo, userInfoUpdateWrapper);
            QueryWrapper<RoleUser> roleUserQueryWrapper = new QueryWrapper<>();
            roleUserQueryWrapper.eq("UserCode", userInfo.getUsrcod());
            roleUserMapper.delete(roleUserQueryWrapper);
            RoleUser roleUser = new RoleUser(userInfo.getRole().getRoleId(), userInfo.getUsrcod());
            roleUserMapper.insert(roleUser);

            List<Fingerprint> fingerprints = userInfo.getFingerprintList();
            for (Fingerprint fingerprint : fingerprints) {
                //如果id>0 更新
                if(fingerprint.getId()>0)
                {
                    fingerprint.setUserId(userInfo.getUsrcod());
                    fingerprint.setUpdateTime(new Date());
                    fingerprintMapper.updateById(fingerprint);
                }
            }
        }
        return userInfo;
    }

    @Override
    public UserInfo deleteUser(UserInfo userInfo) {
        UpdateWrapper<UserInfo> userInfoUpdateWrapper = new UpdateWrapper<>();
        userInfoUpdateWrapper.eq("usrcod", userInfo.getUsrcod());
        userMapper.delete(userInfoUpdateWrapper);
        userMapper.deleteUserAndRole(userInfo.getUsrcod());
        fingerprintMapper.deleteByUserId(userInfo.getUsrcod());
        return userInfo;
    }

    @Override
    public List<UserInfo> getUserInfoList() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<UserInfo> getUsersByPage(Pagination pagination) {
        Page<UserInfo> page = new Page<>(pagination.getCurrent(), pagination.getPageSize());
        IPage<UserInfo> iPage = userMapper.getUsersByPage(page);
        return iPage;
    }

    @Override
    public IPage<UserInfo> getUserInfoListAndFingerPrint(Pagination pagination) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(pagination.getCurrent()-1) * pagination.getPageSize());
        params.put("pageSize",pagination.getPageSize());
        //获取总记录数据
        pagination.setTotal(userMapper.findCountByMap(params));
        IPage<UserInfo> iPage = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());
        iPage.setRecords(userMapper.getUserInfoListAndFingerPrint(params));
        return iPage;
    }


}